package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增购物车内物品
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询购物车内物品
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        //判断购物车内是否已经有该物品
            //有,直接更新原先的shoppingCart，让number+1
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            shoppingCartMapper.update(shoppingCart);
        }
            //没有，插入数据到ShoppingCart
                //菜品
        else if(shoppingCartDTO.getDishId()!=null){
            Dish dish = dishMapper.countById(shoppingCartDTO.getDishId());
            shoppingCart.setDishId(dish.getId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }
        else if (shoppingCartDTO.getSetmealId()!=null) {
            Setmeal setmeal = setmealMapper.findSetmealBysetmealId(shoppingCartDTO.getSetmealId());
            shoppingCart.setSetmealId(setmeal.getId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.addCart(shoppingCart);
    }

    /**
     * 查询购物车内物品
     * @return
     */
    @Override
    public List<ShoppingCart> select() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(currentId);
    }

    /**
     * 删除购物车中的物品
     * @param shoppingCartDTO
     */
    @Override
    public void updateOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.updateOne(shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart shoppingCart1 = list.get(0);
        if(shoppingCart1.getNumber() == 0){
            shoppingCartMapper.delete(shoppingCart1.getId());
        }
    }
}
